﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System.Data;
using ZGL.Api.Data;
using ZGL.Api.Entity;
using ZGL.Api.Models.dto;
using ZGL.Api.Models.vo;
using ZGL.Api.Utils;

namespace ZGL.Api.Services.Role
{
    public class RoleService : IRoleService
    {
        public readonly DataContext DataContext;
        public readonly IMapper Mapper;
        public RoleService(DataContext dataContext, IMapper _mapper)
        {
            DataContext = dataContext;
            Mapper = _mapper;
        }


        public void addRole(RoleAddDto role)
        {
            var Role=Mapper.Map<SysRole>(role);

            
            DataContext.SysRoles.Add(Role);
            DataContext.SaveChanges();
            //角色-菜单表
            List<SysRoleMenuAuth> roleMenuAuth = new List<SysRoleMenuAuth>();
            List<int> menuId = role.menuId;
            foreach (int id in menuId)
            {
                SysRoleMenuAuth sysRoleMenuAuth = new SysRoleMenuAuth();
                sysRoleMenuAuth.MenuAuthId = id;
                sysRoleMenuAuth.CreateBy = role.CreateBy;
                sysRoleMenuAuth.RoleId = Role.RoleId;
                roleMenuAuth.Add(sysRoleMenuAuth);
            }
            DataContext.SysRoleMenuAuths.AddRange(roleMenuAuth);
            DataContext.SaveChanges();
        }

        public void delRole(long id)
        {
            var Role = DataContext.SysRoles.Find(id);
            if (Role == null)
            {
                throw new Exception("无此角色");
            }
            //删除 角色-菜单表
            var roleList = DataContext.SysRoleMenuAuths.Where(r => r.RoleId == id).ExecuteDelete();
            DataContext.SaveChanges();
            DataContext.SysRoles.Remove(Role);
            DataContext.SaveChanges();            
        }

        public SysRoleVo getRole(long id)
        {
            var Role=DataContext.SysRoles.Find(id);
            if (Role==null) {
                throw new Exception("无此角色");
            }
            SysRoleVo SysRole = Mapper.Map<SysRoleVo>(Role);
            //获取 角色-菜单列表
            var roleMenu = DataContext.SysRoleMenuAuths.Where(r => r.RoleId == id).ToList();
            if (roleMenu != null)
            {
                SysRole.RoleMenu = roleMenu;
            }
            return SysRole;
        }

        public Dictionary<string, object> getRoleList(RoleDto roleDto)
        {
            IQueryable<SysRole> list = null;
            int total = 0;
            Dictionary<string, object> Dictionary = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(roleDto.RoleName))
            {
                //list = from s in DataContext.SysUsers where s.Username.Contains(userDto.Username) orderby s.UserId select s;
                list = DataContext.SysRoles.Where(r => r.RoleName.Contains(roleDto.RoleName)).OrderBy(r => r.CreateTime);
            }
            else
            {
                list = DataContext.SysRoles.OrderBy(r => r.CreateTime);
            }
            List<SysRole> items = new List<SysRole>();
            items = list.Skip((roleDto.current - 1) * roleDto.size).Take(roleDto.size).ToList();
            total = list.Count();
            Dictionary.Add("list", items);
            Dictionary.Add("total", total);
            return Dictionary;
        }

        public void putRole(RolePutDto role)
        {
            var RoleTO = DataContext.SysRoles.Find(role.RoleId);
            if (RoleTO == null)
            {
                throw new Exception("无此角色");
            }
            RoleTO.CorpId = role.CorpId;
            RoleTO.CreateBy=role.CreateBy;
            RoleTO.RoleName = role.RoleName;
            RoleTO.RoleDesc = role.RoleDesc;
            RoleTO.RoleType = role.RoleType;
            RoleTO.Status = role.Status;
            RoleTO.CreateBy = role.CreateBy;
            DataContext.SaveChanges();
            //var Roles = Mapper.Map<SysRole>(role);
            //DataContext.SysRoles.Attach(Roles);
            //DataContext.Entry(Roles).State = EntityState.Modified;
            //角色 - 菜单表
            //先 删除  
            var roleList = DataContext.SysRoleMenuAuths.Where(r => r.RoleId == role.RoleId).ExecuteDelete();
            DataContext.SaveChanges();

            //再新增
            List<SysRoleMenuAuth> roleMenuAuth = new List<SysRoleMenuAuth>();
            List<int> menuId = role.menuId;
            foreach (int id in menuId)
            {
                SysRoleMenuAuth sysRoleMenuAuth = new SysRoleMenuAuth();
                sysRoleMenuAuth.MenuAuthId = id;
                sysRoleMenuAuth.CreateBy = role.CreateBy;
                sysRoleMenuAuth.RoleId = role.RoleId;
                roleMenuAuth.Add(sysRoleMenuAuth);
            }
            DataContext.SysRoleMenuAuths.AddRange(roleMenuAuth);
            DataContext.SaveChanges();
        }
    }
}
